<?php

/**
 * Created by IntelliJ IDEA.
 * User: jirapatjantsakool
 * Date: 3/7/14
 * Time: 12:24 AM
 */
require('../model/user.php');

require('../db/database.php');
require('../service/common_service.php');

class login_service extends common_service
{
    public function findUserByUsername($username)
    {
        global $db;
        $user = new user();

        $sql = "SELECT id, "
            . " username, password, displayname, email, "
            . " firstname, lastname, age, birthday, usertype "
            . " FROM go_user "
            . " WHERE username = ? ";

        if ($stmt = $db->prepare($sql)) {

            //binding param
            mysqli_stmt_bind_param($stmt, 's', $username);

            $db->execute_statement($stmt);

            $rowMaps = array();
            mysqli_stmt_bind_result($stmt, $rowMaps['id'],
                                           $rowMaps['username'],
                                           $rowMaps['password'],
                                           $rowMaps['displayname'],
                                           $rowMaps['email'],
                                           $rowMaps['firstname'],
                                           $rowMaps['lastname'],
                                           $rowMaps['age'],
                                           $rowMaps['birthday'],
                                           $rowMaps['usertype']);

            //TODO check number record
            if ($db->fetch_statement($stmt)) {

                $user->setId($rowMaps['id']);
                $user->setUsername($rowMaps['username']);
                $user->setPassword($rowMaps['password']);
                $user->setDisplayname($rowMaps['displayname']);
                $user->setEmail($rowMaps['email']);
                $user->setFirstname($rowMaps['firstname']);
                $user->setLastname($rowMaps['lastname']);
                $user->setAge($rowMaps['age']);
                $user->setBirthday($rowMaps['birthday']);
                $user->setUsertype($rowMaps['usertype']);

            }
            $db->close_connection();
        }
        return $user;
    }
    
    public function isValidateTokenKey($token)
    {
        global $db;
        $common_service = new common_service();
        return $common_service->validateTokenKey($db, $token, 'token');
    }

}

$login_service = new login_service();
//var_dump($login_service->isValidateTokenKey('abc1234'));

?>
